package com.zhuoyue.system.service.impl;

import com.zhuoyue.common.annotation.DataScope;
import com.zhuoyue.system.domain.dto.AccountAuthorizeRepDTO;
import com.zhuoyue.system.mapper.TblAccountAuthorizeMapper;
import com.zhuoyue.system.service.result.ControllerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.zhuoyue.system.domain.TblAccountAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * @author wjming
 */
@Service
public class TblAccountAuthorizeService {
    @Autowired
    private TblAccountAuthorizeMapper tblAccountAuthorizeMapper;

    @Value("${zyzn.auth-api}")
    private String authApi;


    /**
     * 获取授权数据的分页数据
     *
     * @param accountAuthorize
     * @return
     */
    @DataScope(deptAlias = "d", userAlias = "a")
    public List<TblAccountAuthorize> getPageList(TblAccountAuthorize accountAuthorize) {
        List<TblAccountAuthorize> accountAuthorizes = tblAccountAuthorizeMapper.selectPage(accountAuthorize);
        if (CollectionUtils.isEmpty(accountAuthorizes)) {
            return Collections.emptyList();
        }
        return accountAuthorizes;
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    public int deleteByPrimaryKey(String id) {
        return tblAccountAuthorizeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     *
     * @param id
     * @return
     */
    public int deleteButchById(List<String> id) {
        return tblAccountAuthorizeMapper.deleteByListIdUpdateDel(id);
    }

    /**
     * 添加数据
     *
     * @param record
     * @return
     */
    public int insert(TblAccountAuthorize record) {
        return tblAccountAuthorizeMapper.insert(record);
    }

    public int insertOrUpdate(TblAccountAuthorize record) {
        return tblAccountAuthorizeMapper.insertOrUpdate(record);
    }

    public int insertOrUpdateSelective(TblAccountAuthorize record) {
        return tblAccountAuthorizeMapper.insertOrUpdateSelective(record);
    }

    public int insertSelective(TblAccountAuthorize record) {
        return tblAccountAuthorizeMapper.insertSelective(record);
    }

    /**
     * 通过ID获取信息
     *
     * @param id
     * @return
     */
    public AccountAuthorizeRepDTO selectByPrimaryKey(String id) {
        TblAccountAuthorize tblAccountAuthorize = tblAccountAuthorizeMapper.selectByPrimaryKey(id);
        return new AccountAuthorizeRepDTO().doToDto(tblAccountAuthorize);
    }

    public int updateByPrimaryKeySelective(TblAccountAuthorize record) {
        return tblAccountAuthorizeMapper.updateByPrimaryKeySelective(record);
    }


    public int updateBatch(List<TblAccountAuthorize> list) {
        return tblAccountAuthorizeMapper.updateBatch(list);
    }

    public int updateBatchSelective(List<TblAccountAuthorize> list) {
        return tblAccountAuthorizeMapper.updateBatchSelective(list);
    }

    public int batchInsert(List<TblAccountAuthorize> list) {
        return tblAccountAuthorizeMapper.batchInsert(list);
    }

    public Map<String, Object> requestApiToekn(String account, String password, String orgCode) {
        final int httpSuccessCode = 200;
        RestTemplate restTemplate = new RestTemplate();
        String tokenUrl = authApi + "?account=" + account + "&password=" + password
                + "&organizationCode=" + orgCode + "&type=hotel";

        ResponseEntity<ControllerResult> forEntity = restTemplate.getForEntity(tokenUrl, ControllerResult.class);
        Map<String, Object> map = new HashMap<>(5);
        if (forEntity.getStatusCode().value() != httpSuccessCode) {
            map.put("error", "服务器请求失败");
            return map;
        }
        ControllerResult body = forEntity.getBody();
        if (!body.isSuccess()) {
            map.put("error", body.getMsg());
            return map;
        }
        Map<String, Object> data = (Map<String, Object>) body.getDataObject();

        map.put("token", data.get("token"));
        map.put("expirationTime", data.get("expirationTime"));
        return map;
    }
}



